var request = require('request');
global.Moods = db.collection("Moods");
module.exports = {
	get: function(req, res) {
		var data = req.query;
		co(function*(data) {
			var count = yield Moods.count();
			var docs = [];
			if (count > 7) {
				docs = yield Moods.find({}).skip(count - 7).limit(7).toArray();
			} else {
				docs = yield Moods.find({}).limit(7).toArry();
			};
			docs.length ? (docs = docs) : (docs = {});
			console.log(new Date().toLocaleString(), ":get Moods success");
			res.send(docs);
		}).catch(e => {
			console.log(e);
			res.send({});
		});
	},
	post: function(req, res) {
		var data = req.body;
		data.time = new Date().getTime();
		co(function*() {
			var r = yield Moods.insertOne(data);
		}).catch(e => {
			console.log(e);
			res.send({});
		});
		res.send(data);
	},
	put: function(req, res) {
		res.end();
	},
	delete: function(req, res) {
		res.end();
	}
};